package com.ascentive.finallyfast.deepclean;

import java.util.ArrayList;
import android.database.Cursor;
import android.provider.Browser;
import android.util.Log;
import com.ascentive.finallyfast.FinallyFastApplication;

public class BrowserHistory extends DeepCleanListItem {

	int noOfLinksDeleted = 0;

	public BrowserHistory(FinallyFastApplication app, String name, boolean value, int img) {
		super(app, name, value, img);
	}

	@Override
	void doClean() {
		
		ArrayList<String> urlNamesList = new ArrayList<String>();
		Cursor browserHistoryCursor = mApp.getContentResolver().query(Browser.BOOKMARKS_URI,
                Browser.HISTORY_PROJECTION, 
                "date <= (" + Utility.getDateBeforeNDays(mApp.getTargetDateDuration() ) +")", 
                null, null);
       
		// if Inbox is Empty
        if(browserHistoryCursor.getCount() == 0) 
        	return;
        
        browserHistoryCursor.moveToFirst();
        if (browserHistoryCursor.moveToFirst() && browserHistoryCursor.getCount() > 0) {
            while (browserHistoryCursor.isAfterLast() == false) {
            	
            	urlNamesList.add(browserHistoryCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX));
            	
                Log.v("titleIdx", browserHistoryCursor
                        .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
                Log.v("urlIdx", browserHistoryCursor
                        .getString(Browser.HISTORY_PROJECTION_URL_INDEX));

                Log.v("dateIdx", browserHistoryCursor
                        .getString(Browser.HISTORY_PROJECTION_DATE_INDEX));

                browserHistoryCursor.moveToNext();
            }
        }
        noOfLinksDeleted = browserHistoryCursor.getCount();
        
        // Delete all history items from begin to end.
        Browser.deleteHistoryTimeFrame(mApp.getContentResolver(), Utility.getDateBeforeNDays(mApp.getTargetDateDuration() ), -1);
        
        return;
	}

	@Override
	int numberOfFileCleaned() {
		return noOfLinksDeleted;
	}
}

/*
Cursor mCur = activity.managedQuery(Browser.BOOKMARKS_URI,
                    Browser.HISTORY_PROJECTION, null, null, null);
            mCur.moveToFirst();
            if (mCur.moveToFirst() && mCur.getCount() > 0) {
                while (mCur.isAfterLast() == false) {
                    Log.v("titleIdx", mCur
                            .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
                    Log.v("urlIdx", mCur
                            .getString(Browser.HISTORY_PROJECTION_URL_INDEX));
                    mCur.moveToNext();
                }
            }
*/